package com.gy.studyonlineapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gy.studyonlineapp.R;
import com.gy.studyonlineapp.adapter.KnowledgeAdapter;
import com.gy.studyonlineapp.common.Constants;
import com.gy.studyonlineapp.entity.Knowledge;
import com.gy.studyonlineapp.entity.KnowledgeItem;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class KnowledgeActivity extends AppCompatActivity {

    private TextView content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledage);
        content = findViewById(R.id.content);
        //getXxxExtra方法获取Intent传递过来的数据
        String id = getIntent().getStringExtra("objectId");
        // 根据ID进行查询相应的数据
        if (!id.isEmpty()) {
            String url = "http://" + Constants.localhost + ":8080/search-detail";
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("id", id)
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {}
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Gson gson = new Gson();
                    Knowledge knowledge = gson.fromJson(response.body().string(), Knowledge.class);
                    runOnUiThread(() -> {
                        content.setText(knowledge.getContent());
                    });
                }
            });
        }
    }
}